﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace Entangle
{
    /// <summary>
    /// Interaction logic for SConnectorTest.xaml
    /// </summary>
    public partial class SConnectorTest : Window
    {
        ServerConnector server;
        public SConnectorTest()
        {
            InitializeComponent();
        }
        private void TxtLog(string s)
        {
            txtLog.Text += Environment.NewLine;
            txtLog.Text += s;
            txtLog.ScrollToEnd();
        }
        private void btnCreateSrvCon_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                TxtLog("Creating connector...");
                server = new ServerConnector(txtServAdd.Text);
                TxtLog("Created.");
                brdServAdd.Visibility = System.Windows.Visibility.Hidden;
                brdParams.Visibility = System.Windows.Visibility.Visible;
                brdActions.IsEnabled = true;
            }
            catch (Exception ex) {
                brdActions.IsEnabled = false;
            }
        }

        private void btnSynch_Click(object sender, RoutedEventArgs e)
        {
            TxtLog(String.Format("Calling synch(synchKey={0}, publicKey={1})...",txtParam1.Text,txtParam2.Text));
            try
            {
                string r = server.Synch(txtParam1.Text, txtParam2.Text);
                TxtLog("Got response:" + r);
            }
            catch (Exception ex)
            {
                TxtLog("Failed!");
            }

        }

        private void btnClear_Click(object sender, RoutedEventArgs e)
        {
            txtLog.Text = String.Empty;
        }

        private void btnRequest_Click(object sender, RoutedEventArgs e)
        {
            TxtLog(String.Format("Calling request(xml={0}, userId={1})...", txtParam1.Text, txtParam2.Text));
            try
            {
                string r = server.Request(txtParam1.Text, int.Parse(txtParam2.Text)); ;
                TxtLog("Got response:" + r);
            }
            catch (Exception ex)
            {
                TxtLog("Failed!");
            }
        }

        private void btnCheck_Click(object sender, RoutedEventArgs e)
        {
            TxtLog(String.Format("Calling check(userId={0}, timestamp={1})...", txtParam1.Text, txtParam2.Text));
            try
            {
                string r = server.Check(int.Parse(txtParam1.Text), DateTime.Parse(txtParam2.Text)); ;
                TxtLog("Got response:" + r);
            }
            catch (Exception ex)
            {
                TxtLog("Failed!");
            }
        }

        private void btnGet_Click(object sender, RoutedEventArgs e)
        {
            TxtLog(String.Format("Calling get(userId={0}, rid={1})...", txtParam1.Text, txtParam2.Text));
            try
            {
                string r = server.Get(int.Parse(txtParam1.Text), int.Parse(txtParam2.Text)); ;
                TxtLog("Got response:" + r);
            }
            catch (Exception ex)
            {
                TxtLog("Failed!");
            }
        }
    }
}
